package com.candy.shoporder.controller;

import com.candy.entity.Order;
import com.candy.shoporder.feignService.ProductFeignService;
import com.candy.shoporder.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/orders")
@RefreshScope
public class OrderController2 {
    @Autowired
    OrderService orderService;

    @Autowired
    ProductFeignService productFeignService;

    @RequestMapping("/addOrderByOpenFeign")
    @GlobalTransactional
    public String addOrder(){
        Order order=new Order();
        order.setUserId(1);
        order.setPrice(61.5);
        order.setProductId(1);
        order.setAmount(61.5);
        order.setNumber(1);
        order.setUsername("candy");
        order.setProductName("Java从入门到精通");
        orderService.addOrder(order);
//使用openfeign调用product服务
        String productMethodResponse=productFeignService.editProduct(order.getProductId());
        System.out.println(5/0);
        return "下单成功!"+productMethodResponse;
    }

    @RequestMapping("/test")
    public String test() throws InterruptedException {
//        Thread.sleep(1000);
        return "下单成功";
    }
}
